package com.zi.experttalk.experttalk.View;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import com.zi.experttalk.experttalk.Model.entity.Merchant;
import com.zi.experttalk.experttalk.Model.service.HttpManager;
import com.zi.experttalk.experttalk.R;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lenovo on 2014/12/10.
 */
public class CouponActivity extends Activity {
   List<Map<String,Object>> mapList=new ArrayList<Map<String, Object>>();
   List<Merchant> merchantList=new ArrayList<Merchant>();
   private ListView coupon_shopsItem_listview;
   private String couponListParam="{\"city_id\":\"1\",\"pwd\":\"\",\"sort\":\"0\",\"page\":\"1\",\"email\":\"\",\"cate_id\":\"0\",\"m_longitude\":\"108.90913391113281\",\"m_latitude\":\"34.237911224365234\",\"quan_id\":\"0\",\"act\":\"youhuilist\",\"key\":\"\"}";
   private  String initParam="{\"act\":\"init\"}";
   private String param="{\"id\":\"75\",\"pwd\":\"123456\",\"act_2\":\"\",\"act\":\"merchantitem\",\"email\":\"cdm123\"}";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.coupon);
        coupon_shopsItem_listview =(ListView)findViewById(R.id.coupon_shopsItem_listview);
    /*
    优惠商户列表
     */
//        Map<String,Object> map=new HashMap<String, Object>();
//
//        map.put("name","老孙家大甩卖");
//        map.put("address","西安钟楼");
//        mapList.add(map);
//
//        map=new HashMap<String, Object>();
//
//        map.put("name","李宁打折扣");
//        map.put("address","西安高新");
//        mapList.add(map);
//        SimpleAdapter simpleAdapter=new SimpleAdapter(this,mapList,
//                R.layout.coupon_merchant_item,
//                new String[]{"name","address"},
//                new int[]{R.id.coupon_mItem_mName_textview,R.id.coupon_mItem_mAddress_textview});
//        coupon_shopsItem_listview =(ListView)findViewById(R.id.coupon_shopsItem_listview);
//        coupon_shopsItem_listview.setAdapter(simpleAdapter);

        HttpManager httpManager=new HttpManager();

            //获取数据并JSON解析
            try {
                httpManager.getHttpInfo(couponListParam,new HttpManager.GetDataResultListener() {
                    @Override
                    public void onResult(String data) {
                       if(data!=null){
                           Log.d("data",data);
                           try {
                               addData(data);
                           } catch (JSONException e) {
                               Toast.makeText(CouponActivity.this,"解析有误",Toast.LENGTH_SHORT).show();
                           }
                       }
                    }
                });
            }catch (IOException e) {
                    Toast.makeText(this,"网络请求失败",Toast.LENGTH_SHORT).show();
            }
       /*
       点击商户，进入优惠详情列表
        */
        coupon_shopsItem_listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                 Map map=new HashMap<String, Object>();
                 map=mapList.get(position);
                 todetails(map);
            }
        });
    }
    private void addData(String getedData) throws JSONException {
        JSONObject jsonObject=new JSONObject(getedData);
        JSONArray listArray=jsonObject.optJSONArray("item");
        for (int i = 0; i <listArray.length() ; i++) {
            JSONObject itemObject=listArray.optJSONObject(i);
            String id=itemObject.getString("id");
            String title=itemObject.getString("title");
            String logo=itemObject.getString("logo");
            String merchant_logo=itemObject.getString("merchant_logo");
            String address=itemObject.getString("address");
            Merchant merchant=new Merchant();
            merchant.setId(id);
            merchant.setTitle(title);
            merchant.setLogo(logo);
            merchant.setMerchant_logo(merchant_logo);
            merchant.setAddress(address);
            merchantList.add(merchant);
    }
        //数据写入Adapter
        for (int j = 0; j <merchantList.size() ; j++) {
            Map<String,Object> map=new HashMap<String, Object>();
            Merchant merchant=merchantList.get(j);
            map.put("name",merchant.getTitle());
            map.put("address",merchant.getAddress());
            mapList.add(map);
        }
        final SimpleAdapter simpleAdapter=new SimpleAdapter(this,mapList,
                R.layout.coupon_merchant_item,
                new String[]{"name","address"},
                new int[]{R.id.coupon_mItem_mName_textview,R.id.coupon_mItem_mAddress_textview});
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                coupon_shopsItem_listview.setAdapter(simpleAdapter);
            }
        });
    }
    private void todetails(Map map){
        Intent intent=new Intent(CouponActivity.this,CouponDetailsActivity.class);
        intent.putExtra("mName",(String)map.get("name"));
        intent.putExtra("mAddress",(String)map.get("address"));
        startActivity(intent);
    }
}
